#include "wangge.h"
#include "ui_wangge.h"
#include<QPainter>
#include <stdio.h>
#include<QTime>
#include<QPen>
#include<QString>
int b=1,k=300,h=100+rand()%200,c=1,l=0;

wangge::wangge(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::wangge)
{
    ui->setupUi(this);
    resize(500,500);
    time = new QLabel(this);
    time->resize(100,455);
    time->move(245,100);
    time->setText("1234");
    int i=0;
    x[0]=100;
    for(i=0;i<11;i++)
    {
        x[i]=100+20*i;
        y[i]=300;
    }
    i=0;
    x[0]=100;
    for(i=0;i<11;i++)
    {
        x[i]=100+20*i;
        z[i]=300;
    }
    this->startTimer(200);
}

wangge::~wangge()
{
}
void wangge::paintEvent(QPaintEvent *)
{
    int i,a=100;
    QPainter painter(this);
    QPen pen(Qt::black);
    painter.setPen(pen);
    painter.drawLine(100,100,100,300);
    painter.drawLine(100,300,300,300);
    painter.drawLine(100,100,300,100);
    painter.drawLine(300,100,300,300);
    QPen pena(Qt::DotLine);
    painter.setPen(pena);
    for(i=0;i<10;i++)
    {
        painter.drawLine(100,a,300,a);
        a=a+20;
    }
    a=100;
    for(i=0;i<10;i++)
    {
        painter.drawLine(a,100,a,300);
        a=a+20;
    }
    QPen pencil(Qt::red);
    painter.setPen(pencil);
    painter.drawLine(345,150,365,150);
    a=100;
    for(i=0;i<10;i++)
    {
        painter.drawLine(x[i],y[i],x[i+1],y[i+1]);
    }
    QPen penc(Qt::green);
    painter.setPen(penc);
    painter.drawLine(345,170,365,170);
    for(i=0;i<10;i++)
    {
        painter.drawLine(x[i],z[i],x[i+1],z[i+1]);
    }

}

void wangge::set_cpu(QString s)
{
    this->time->setText(s);
    QString p=s;
    int i;
    int q=p.toDouble();
    if(l%2==0)
    {
    for(i=0;i<10;i++)
    {
       y[i]=y[i+1];
    }  
    if(b==1)
    {
       y[10]=300;
       b=2;
    }
    else
    {
       y[10]=300-2*q;
    }
    l++;
    for(i=0;i<10;i++)
    {
       z[i]=z[i+1];
    }
    z[10]=z[9];
    }
    else
    {
    for(i=0;i<10;i++)
    {
       z[i]=z[i+1];
    }
    if(b==1)
    {
       z[10]=300;
       b=2;
    }
    else
    {
       z[10]=300-2*q;
    }
    l++;
    for(i=0;i<10;i++)
    {
       y[i]=y[i+1];
    }
    y[10]=y[9];
    }
    this->update();
}

